import 'package:azlistview/azlistview.dart';
import 'package:flutter/material.dart';
import 'package:magic_car/util/HttpHelper.dart';
class CartypeSelectRoute extends StatefulWidget {
  final Map param;
  final String bid;
  CartypeSelectRoute(this.param,this.bid);
  @override
  State<StatefulWidget> createState() {
    return new _BrandSelectRouteState(this.param,this.bid);
  }
}

class _BrandSelectRouteState extends State<CartypeSelectRoute> {
  // List<CityInfo> _cityList = List();
  // List<CityInfo> _hotCityList = List();
  final Map param;
  final String bid;
  _BrandSelectRouteState(this.param,this.bid);
  List BrandList=List();
  List hotList = List();
  List<double>heights = List();
  ScrollController scrollController = new ScrollController();

  int _suspensionHeight = 40;
  int _itemHeight = 50;
  String _suspensionTag = "";

  @override
  void initState() {
    super.initState();
    scrollController.addListener(() {
      double position = scrollController.offset.toDouble();
      // int index = _computerIndex(position);
      // setState(() {
      //  // defaultIndex = index;
      // });
    });
    loadData();
  }
  // int _computerIndex(double position) {
  //   if (widget.data != null) {
  //     for (int i = 0; i < widget.data.length - 1; i++) {
  //       double pre = _computerIndexPosition(i);
  //       double next = _computerIndexPosition(i + 1);
  //       if (position > pre && position < next) {
  //         return i;
  //       }
  //     }
  //   }
  //   return 0;
  // }

  // double _computerIndexPosition(int index) {
  //   int n = 0;
  //   if (widget.data != null) {
  //     for (int i = 0; i < index; i++) {
  //       n += (widget.data[i]['userList'].length).toInt();
  //     }
  //   }
  //   return n * widget.itemHeight + (index + 1) * widget.suspensionHeight;
  // }
  void loadData() async {

    Map<String, String> map = new Map();
    map["bid"]=this.bid;
    HttpHelper.post(HttpHelper.API_CarType,(data) async {
      //loadpage.close();
      if (data != null) {
        print(data);
        //Toast.show(data["msg"], context);

        if (data["code"].toString() == "1") {
         List temp = data["info"];
         setState(() {
           BrandList = temp;
         });
       //           print('1111');
         // List temp = data["info"];
         

        }
      }
    }, params: map);

  }

 Widget layout() {
    List<Widget> tiles = [];
    Widget content;

    for (var item in BrandList) {
     
      
      tiles.add(
        
       GestureDetector(
            onTap: (){
              param["value"]=item["name"];
              param["type_id"]=item["id"];
              Navigator.pop(context);
            },
            child:
        new Container(
          //color: Colors.red,
          height: 50,
          width: double.infinity,
          decoration: new BoxDecoration(
    border: new Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)) ),
    
   // borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
   
  ),


          child:
          new Padding(padding: new EdgeInsets.fromLTRB(10, 0, 0, 0),child:
          
          new Row(
            children: <Widget>[
              Image.asset('resource/images/jiance/carmodel1.jpeg',width: 30,height: 30,),
              Padding(padding:new EdgeInsets.fromLTRB(5, 0, 10, 0),child:
              Text(item["name"]))
             //Image.network(item["pic_url"],width: 100,height: 30,)
            ]
          ))),
        )
      );
    }

    content = new Padding(
        padding: new EdgeInsets.fromLTRB(10, 0, 10, 10),
        child: new Column(
            children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
            //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
            ));
    return content;
  }
Widget buildcontent(item){
  List<Widget> tiles = [];
    Widget content;
    var list = item["userList"];
    for(var items in list){
      tiles.add(
        Padding(padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),child:
        Container(
          
          width: double.infinity-50,
          height:50,child:
          GestureDetector(
            onTap: (){
              param["value"]=items["name"];
              param["brand_id"]=items["id"];
              Navigator.pop(context);
            },
            child:
        new Row(
            children: <Widget>[
              Image.asset('resource/images/jiance/carmodel1.jpeg',width: 100,height: 50,),
              Padding(padding: new EdgeInsets.fromLTRB(10, 0, 0, 0),child:
              Text(items["name"]))
             //Image.network(item["pic_url"],width: 100,height: 30,)
            ]
          ))))
      );
    }
    content = new Padding(
        padding: new EdgeInsets.fromLTRB(0, 20, 0, 20),
        child: new Column(
            children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
            //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
            ));
    return content;
}
Widget indexbar(){
  List<Widget> tiles = [];
    Widget content;
   // var list = item["userList"];
    for(var i=0;i<BrandList.length;i++ ){
      var item = BrandList[i];
      tiles.add(
        Padding(padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),child:
        GestureDetector(
          onTap: (){
            print('点击索引'+i.toString());
            double y = 0.0;
            for(var j =0;j<heights.length;j++){
             // print(he)
            if(j<i)y+=heights[j];
            }
            print(y.toString());
            scrollController.animateTo(y, duration: new Duration(seconds: 1), curve: Curves.ease);
          },
          child:
        Container(
          
          width: double.infinity-50,
          height:20,child:
          Text(item["tag"])
        
      ))));
    }
    content = new Padding(
        padding: new EdgeInsets.fromLTRB(0, 20, 0, 20),
        child: new Column(
            children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
            //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
            ));
    return content;
}
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('车型'),
          backgroundColor: Colors.white,
          elevation: 0,
          // backgroundColor: Color.fromRGBO(255, 204, 48, 1),
        ),
        body: new Center(
            child: Container(
                decoration: new BoxDecoration(
                  //color: Color.fromRGBO(238, 238, 238, 1),
                ),
                child: Padding(
                    padding: new EdgeInsets.fromLTRB(0, 15, 0, 0),
                    child: Stack(
                      children: <Widget>[
                        Align(
                          alignment: Alignment.centerLeft,
                          child: new ListView(
                            controller: scrollController,
                            children: <Widget>[layout()]),
                        ),
                        // Align(
                          
                        //     alignment: Alignment.topRight,
                            
                        //     child: new Container(
                        //       width:30,
                        //       child:
                        //     indexbar(),
                        //     ))
                      ],
                    )
                    // new ListView(
                    //   children: <Widget>[buildcontent()],
                    // )),
                    ))));
  }
}
class Suspension extends StatelessWidget {
  final String tag;
  final double suspensionHeight;

  Suspension({this.tag, this.suspensionHeight});

  @override
  Widget build(BuildContext context) {
    return Offstage(
      child: new Container(
        child: new Text(
          "$tag",
        ),
        alignment: Alignment.centerLeft,
        margin: const EdgeInsets.only(left: 16.0),
        height: suspensionHeight,
      ),
      offstage: false,
    );
  }
}

